
#include <pic18.h>
#include "my_macros.h"
//volatile near bit TimeI_10ms;
volatile near bit TimeI_2ms;

//near bit Time_1ms;
near bit Time_2ms;
near bit Time_10ms;
near bit Time_100ms;
near bit Time_500ms;
near bit Time_1000ms;
near bit Time_5000ms;

/* 
	===== TIMER0 =====
	bit 2-0 T0PS<2:0>: Timer0 Prescaler Select bits
	111 = 1:256 prescale value
	110 = 1:128 prescale value
	101 = 1:64 prescale value
	100 = 1:32 prescale value
	011 = 1:16 prescale value
	010 = 1:8 prescale value
	001 = 1:4 prescale value
	000 = 1:2 prescale value
 */


void t0_init ( void )
{
	/* System Clock Init 18*/
	TMR0 = 0xFFF0;
	TMR0IF = 0; 
	TMR0IE = 1;
	T0CON = 0b10000100;	
}

void t1_init ( void )
{
	TMR1 = 1;
	T1CON = 0b10100001;	
}


void SysClock_Init ( void )
{
	t0_init();
	t1_init();
}

unsigned int SysClock_Counter;

void SysTime( void )
{

	Time_2ms = FALSE;
	Time_10ms = FALSE;
	Time_100ms = FALSE;
	Time_500ms = FALSE;
	Time_1000ms = FALSE;
	Time_5000ms = FALSE;

	if ( TimeI_2ms )
	{
		TimeI_2ms = 0;

		Time_2ms = TRUE;

		SysClock_Counter++;
		

		if ( !(SysClock_Counter % 5)  )
		{
			Time_10ms = TRUE;
		}

		if ( !(SysClock_Counter % 50)  )
		{
			Time_100ms = TRUE;
		}

		if ( !(SysClock_Counter % 250)  )
		{
			Time_500ms = TRUE;
		}

		if ( !(SysClock_Counter % 500)  )
		{
			//SysClock_Counter = 0;
			Time_1000ms = TRUE;
		}

		if ( !(SysClock_Counter % 2500)  )
		{
			Time_5000ms = TRUE;
		}

	}
} 
